/**
 * author:Administrator
 * date:2013-5-21
 */
package com.roc.web.action.setting;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction; // import com.roc.pojo.Merchant;
import com.roc.pojo.Operators;
import com.roc.pojo.SettingBean; // import com.roc.pojo.ProfitRule;
import com.roc.service.IPagenitionService; // import
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.service.setting.ISettflagService;
import com.roc.util.ApplicationContextUtils;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.web.action.MerchantSearchCondition;
import com.roc.web.timer.CalcuUtil;

/**
 * @author Feng haitao
 * 
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/settingOper")
public class SettingOperationAction extends PaginationBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5236170890634487270L;
	private Logger logger = LogManager.getLogger(SettingOperationAction.class);
	private Operators operators;
	private List<Object> profitRsList = new ArrayList<Object>();
	private SettingSearchCondition searchCondition;

	private String msg;
	private String modelFlag;
	private String minDate;
	private String maxDate;
	@Autowired
	IPagenitionService pagenitionService;

	@Autowired
	IorgService orgService;
	@Autowired
	ISettflagService settflagService;
	CalcuUtil calcuUtil=null;
	// 结算操作
	@Action(value = "upload", results = {
			@Result(name = "success", location = "/pages/setting/settingOper.jsp"),
			@Result(name = "error", location = "/pages/setting/settingOper.jsp"),
			@Result(name = "login", location = "/pages/redirect.jsp") })
	public String upload() {
		Operators logingMerchant = (Operators) getSession().getAttribute(
				Constant.SESSION_LOGIN_MERCHANT);
		int i =0;
		if(StringUtils.isNotBlank(minDate)){
			minDate = minDate.replace("-", "");
		}
		if(StringUtils.isNotBlank(maxDate)){
			maxDate = maxDate.replace("-", "");
		}
		try {
			if("1".equals(modelFlag)){
				i=settflagService.excuKQSettingPro(minDate,maxDate);
			}else if("2".equals(modelFlag)){
				i=settflagService.excuHRTSettingPro(minDate,maxDate);
			}
			if(i==1){
				String str="结算执行完毕！";
				addActionMessage(str);
				if(calcuUtil==null){
					calcuUtil = (CalcuUtil) ApplicationContextUtils
					.getBean("calcuUtil");
				}
				calcuUtil.excuDiaodan();
			}else{
				addActionMessage("结算执行失败！");
			}
			
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	// 结算初始页面
	@Action(value = "settingOperinit", results = {
			@Result(name = "success", location = "/pages/setting/settingOper.jsp"),
			@Result(name = "login", location = "/pages/redirect.jsp"),
			@Result(name = "error", location = "/error.jsp") }

	)
	public String settingOperlist() throws Exception {
		// List settingflagList=new ArrayList();
		// settingflagList=settflagService.querySettingFlagList();
		// getRequest().setAttribute("settingflagList", settingflagList);
		return SUCCESS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSession()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (SettingSearchCondition) getSession().getAttribute(
				SEARCH_SESSION_BEAN_SETTING);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSubClass()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#setSearchConditionToSubClass(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.roc.page.PaginationBaseAction#setSearchToConditionSession(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_BEAN_SETTING, searchCondition);
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg
	 *            the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the profitRsList
	 */
	public List<Object> getProfitRsList() {
		return profitRsList;
	}

	/**
	 * @param profitRsList
	 *            the profitRsList to set
	 */
	public void setProfitRsList(List<Object> profitRsList) {
		this.profitRsList = profitRsList;
	}

	public String getMinDate() {
		return minDate;
	}

	public void setMinDate(String minDate) {
		this.minDate = minDate;
	}

	public String getMaxDate() {
		return maxDate;
	}

	public void setMaxDate(String maxDate) {
		this.maxDate = maxDate;
	}

	public String getModelFlag() {
		return modelFlag;
	}

	public void setModelFlag(String modelFlag) {
		this.modelFlag = modelFlag;
	}

	/**
	 * @return the profitModelHead
	 */

	public Operators getOperators() {
		return operators;
	}

	public void setOperators(Operators operators) {
		this.operators = operators;
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return null;
	}

	public SettingSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(SettingSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

}
